*genMicroData
version 12

use "./orgdata/ess1e06_2", clear

*Importing incomevariables from separate country files for France, Ireland and Hungary as observations from these countries does not have info on income in CC-data
clonevar temp = idno
drop idno
gen idno =_n+40000
replace idno = temp if cntry =="FR"
merge 1:1 idno using "./orgdata/ESS1FR_special", keepusing(hinctnfr)
drop _merge

drop idno
gen idno =_n+40000
replace idno = temp if cntry =="HU"
merge 1:1 idno using "./orgdata/ESS1HU_special", keepusing(hinctnhu)
drop _merge

drop idno
gen idno =_n+25000
replace idno = temp if cntry =="IE"
merge 1:1 idno using "./orgdata/ESS1IE_special", keepusing(hinctnie)
drop _merge
drop idno
clonevar idno=temp 
drop temp
order name - cntry idno

*immigrant
recode brncntr (1=1 native) (2=0 immigrant) (else=.a "NA/DK"), gen(native1)
clonevar native2 = native1 
replace native2 = 1 if facntr == 1 & mocntr  == 1
clonevar native3 = native1
replace native3 = 0 if facntr == 2 & mocntr  == 2
lab var native1 "Born in country"
lab var native2 "Born in country or dad+mom born in country"
lab var native3 "Born in country and dad+mom born in country"

keep if native2==1


*Gender
codebook gndr
recode gndr (2=0 female) (1=1 male) (9=.a NoAnswer),gen(male)
codebook male
lab var male "Gender (1=male)"

*age
codebook agea  yrbrn,tab(200)
tab1 agea,nolab
tab inwyr
codebook inwyr
recode inwyr (9999=2002),gen(intyear)
gen age =  intyear-yrbrn if yrbrn<7777
lab var age "Age"
notes age: unknown year of interview is recoded to 2002 (the most prevaliant year)

*contact (friends)
codebook  imgfrnd
recode imgfrnd (1=3 several) (2=2 few) (3=1 none) (7 9=.a NA) (8=.b DK),gen(ifriends)
lab var ifriends "Immigrant friends"
*contact (colleagues)
codebook imgclg
recode imgclg (1=4 several) (2=3 few) (3/4=2 "none/not working") (7 9=.a NA) (8=.b DK),gen(icolleagues1)
*Alt measures
recode imgclg (1=4 several) (2=3 few) (3=2 none) (4=1 "not currently working") (7 9=.a NA) (8=.b DK),gen(icolleagues2)
recode imgclg (1=4 several) (2=3 few) (3=2 none) (4=.c "not currently working") (7 9=.a NA) (8=.b DK),gen(icolleagues3)
lab var icolleagues1 "Immigrant colleagues;3KatCollapsed"
lab var icolleagues2 "Immigrant colleagues;4Kat"
lab var icolleagues3 "Immigrant colleagues;3KatDropped"

*income
codebook hinctnt  hinctnfr hinctnhu hinctnie,ta(100)
recode hinctnt (1=1 <1800) (2=2 1800-3600) (3=3 3600-6000) (4=4 6-12000) (5=5 12-18000) ///
	(6=6 18-24000) (7=7 24-30000) (8=8 30-36000) (9=9 36-60000) (10=10 60-90000) (11=11 90-120000) ///
	(12 = 12 >120000) (77 99=.a NA) (88=.b DK) (.=.c "divient catagories"),gen(iincome1)
replace iincome1 = .a if hinctnfr == 77 | hinctnhu  == 77 | hinctnhu  == 99 | hinctnie ==99
replace iincome1 = .b if hinctnfr == 88 | hinctnhu == 88

recode hinctnt (1=1 <1800) (2=2 1800-3600) (3=3 3600-6000) (4=4 6-12000) (5=5 12-18000) ///
	(6=6 18-24000) (7=7 24-30000) (8=8 30-36000) (9=9 36-60000) (10=10 60-90000) (11=11 90-120000) ///
	(12 = 12 >120000) (77 99= 77 NA) (88= 88 DK) (.= 101 "divient catagories"),gen(iincome2)
replace iincome2 = 77 if hinctnfr == 77 | hinctnhu  == 77 | hinctnhu  == 99 | hinctnie ==99
replace iincome2 = 88 if hinctnfr == 88 | hinctnhu == 88

recode hinctnt (1=1 <1800) (2=2 1800-3600) (3=3 3600-6000) (4=4 6-12000) (5=5 12-18000) ///
	(6=6 18-24000) (7=7 24-30000) (8=8 30-36000) (9=9 36-60000) (10=10 60-90000) (11=11 90-120000) ///
	(12 = 12 >120000) (77 88 99= 77 "DK/NA") (.= 101 "divient catagories"),gen(iincome3)
replace iincome3 = 77 if hinctnfr == 77 | hinctnfr == 88 | hinctnhu  == 77 | hinctnhu  == 99 | hinctnhu  == 88 | hinctnie ==99

clonevar temp = iincome1   
replace temp = hinctnfr if cntry =="FR" & iincome1 == .c
replace temp = hinctnhu if cntry =="HU" & iincome1 == .c
replace temp = hinctnie if cntry =="IE" & iincome1 == .c
gen iincome4a = iincome2

levelsof cntry,local(country)
foreach ctry in `country' {
	sum temp if cntry == "`ctry'",d
	dis "`ctry': N = "`r(N)' ", Mean = " `r(mean)' ",Min/max: "`r(min)' " "`r(max)'
	dis `r(p25)'
	replace iincome4a = 1 if temp < 	`r(p10)' & cntry == "`ctry'"
	replace iincome4a = 2 if temp >= `r(p10)' & temp < `r(p50)' & cntry== "`ctry'"
	replace iincome4a = 3 if temp >= `r(p50)' & temp < `r(p90)' & cntry== "`ctry'"
	replace iincome4a = 4 if temp >= `r(p90)' & temp < . & cntry== "`ctry'"
}
recode iincome4a (1=1 "Below 10p") (2=2 10-50p) (3=3 50-90p) (4=4 "Above 90p") (else=5 "No Info"),gen(iincome4)
label var iincome1 "HHincome,Euro,all missing"
label var iincome2 "HHincome,Euro,none missing"
label var iincome3 "HHincome,Euro,none missing"
label var iincome4 "HHincome,relative,no missing"

drop iincome4a temp


*LR selplacement
codebook lrscale,ta(33)
recode lrscale (0=0 left) (10 = 10 right) (77 99 = .a NA) (88 = .b DK), gen(lrself1)
recode lrscale (0=0 left) (10 = 10 right) (77 99 = .a NA) (88 = 5), gen(lrself2)
lab var lrself1 "Selfplacement on LR-scale"
lab var lrself2 "Selfplacement on LR-scale, DK=5"

*education
codebook edulvla eduyrs, ta(100)
clonevar iedulvl = edulvla
recode iedulvl (6/. = 6)
label define edulvla 6 "No info", add
label define edulvla .a "No info", add
clonevar iedulv2 = iedulvl
recode iedulv2 (6=.a)
lab var iedulvl "Educational level; 6KAT"
lab var iedulv2 "Educational level; 5KAT"



*AFS
codebook  imsmetn imdfetn eimrcnt eimpcnt imrcntr impcntr imunplv imsmrgt imscrlv ///	 
	imacrlv imtcjob imbleco imbgeco imueclt imwbcnt imwbcrm ctbfsmv imrsprc imsetbs imsetmr imdetbs imdetmr alwspsc stimrdt, ta(100)

codebook imwgdwn idetalv imtcjob imbleco imueclt imwbcnt imwbcrm imdetbs imdetmr ,ta(100)

recode imwgdwn (1=10) (2=7.5) (3=5) (4=2.5) (5=0) (else=.), gen(IMimwgdwn)
su IMimw*
recode idetalv (1=10) (2=5) (3=0) (4=0) (else=.),gen(IMidetalv)
ta IMid

recode imsetbs imsetmr imdetbs imdetmr (11/. = .), prefix(IM)

recode imtcjob imbleco imbgeco imueclt imwbcnt imwbcrm (11/.=.), prefix(IM)
foreach var of varlist IMimtcjob-IMimwbcrm {
	replace `var' = (`var'*(-1))+10
	sum `var'
}


recode imsmrgt (6/.=.),prefix(IM)
foreach var of varlist IMimsmrgt {
	replace `var' = ((`var'-1)/(4))*10
	sum `var'
}


recode imunplv  imscrlv  stimrdt (6/.=.), prefix(IM)
foreach var of varlist IMimunplv-IMstimrdt {
	replace `var' = ((`var'-1)/(4))*(-10)+10
	sum `var'
}


recode imsmetn imdfetn eimrcnt eimpcnt imrcntr impcntr (5/.=.),prefix(IM)
foreach var of varlist IMimsmetn-IMimpcntr {
	replace `var' = ((`var'-1)/(3))*(10)
	sum `var'
}


qui: alpha IMimdetbs IMimdetmr IMimtcjob IMimbleco IMimbgeco IMimueclt IMimwbcnt IMimwbcrm IMimsmrgt IMimunplv IMimscrlv IMstimrdt IMimdfetn IMeimpcnt IMimpcntr, gen(AFS) min(10)
lab var AFS "Antiforeigner Sentiment"
sum AFS


*Cleaning up
encode cntry ,gen(counnum) label(country)
keep cntry idno native1-counnum
drop intyear
order cntry counnum

tempfile tf1 tf2
save `tf1'


***************************Dont know******
use "./orgdata/ess1e06_2.dta", clear


****macroes
global r1 "(8=1) (else=0),"
global r2 "(88=1)  (else=0),"
global r3 "(888=1) (else=0),"
global r4 "(8888=1) (else=0),"


*************************
recode brncntr (1=1 native) (2=0 immigrant) (else=.a "NA/DK"), gen(native1)
clonevar native2 = native1 
replace native2 = 1 if facntr == 1 & mocntr  == 1
drop if native2!=1
drop native2 native1
*happyness/wellbeing
*7/9
*inmdisc health hlthhmp
sum inmdisc inmdisc health hlthhmp
recode inmdisc health hlthhmp $r1 pre(HAPPY) 
*77/99
*stflife happy 
sum stflife happy 
recode stflife happy $r2 pre(HAPPY)

*personal
*7/9
*crmvct rlgblg dscrgrp
sum crmvct rlgblg dscrgrp
recode crmvct rlgblg dscrgrp $r1 pre(PERS)

*factual personal
*7/9
*ctzcntr  brncntr blgetmg facntr mocntr empl gndr domicil uemp3m mbtru marital martlfr 
sum ctzcntr  brncntr blgetmg facntr mocntr empl gndr domicil uemp3m mbtru marital martlfr hincfel brwmny
recode ctzcntr  brncntr blgetmg facntr mocntr empl gndr domicil uemp3m mbtru marital martlfr $r1 pre(FACT)

*77/99
*hhmmb edulvla eduyrs mnactic hincsrc  edulvlfa 
sum hhmmb edulvla eduyrs mnactic hincsrc edulvlfa
recode hhmmb edulvla eduyrs mnactic hincsrc  edulvlfa $r2 pre(FACT)

*777/999
*yrlvdae 
recode yrlvdae $r3 pre(FACT)
*7777/9999
recode yrbrn $r4 pre(FACT)

*activities
*77/99
*tvtot tvpol  rdtot rdpol nwsptot nwsppol netuse prtvtat-prtvtsi sclmeet rlgatnd pray 
sum tvtot tvpol  rdtot rdpol nwsptot nwsppol netuse prtvtat-prtvtsi sclmeet rlgatnd pray prtmbat-prtmbsi hlpppl
recode tvtot tvpol  rdtot rdpol nwsptot nwsppol netuse prtvtat-prtvtsi sclmeet rlgatnd pray $r2 pre(ACT)

*7/9
*vote contplt-ilglpst mmbprty  prtmbat-prtmbsi hlpppl atncrse sclact 
sum vote contplt-ilglpst mmbprty atncrse sclact 
recode vote contplt-ilglpst mmbprty  prtmbat-prtmbsi hlpppl atncrse sclact $r1 pre(ACT)


*Values
*77/99
*ppltrst-pplhlp trstprl-trstun prtclat-prtclsi rlgdgr impfml-impvo
sum ppltrst-pplhlp trstprl-trstun prtclat-prtclsi rlgdgr impfml-impvo
recode ppltrst-pplhlp trstprl-trstun prtclat-prtclsi rlgdgr impfml-impvo $r2 pre(VAL)

*7/9
*clsprty prtdgcl aesfdrk 
sum clsprty prtdgcl aesfdrk 
recode clsprty prtdgcl aesfdrk $r1 pre(VAL)


*Interested in politics/self efficacy
*7/9
*polintr polcmpl polactiv poldcs pltcare  pltinvt  discpol 
sum polintr polcmpl polactiv poldcs pltcare  pltinvt  
recode polintr polcmpl polactiv poldcs pltcare  pltinvt  discpol $r1 pre(INT)

*attitudes
*77/99
*lrscale stfeco - stfhlth  impsppl-impapol 
sum lrscale stfeco - stfhlth  impsppl-impapol 
recode lrscale stfeco - stfhlth  impsppl-impapol $r2 pre(ATT)

*7/9
*dclenv -dclintr ginveco-scnsenv
sum dclenv -dclintr ginveco-scnsenv
recode dclenv -dclintr ginveco-scnsenv $r1 pre(ATT)


*immi
*7/9
codebook  imgetn eimgrpc imgrpc imsmetn imdfetn eimrcnt eimpcnt imrcntr impcntr imwgdwn-imacrlv ctbfsmv imrsprc pplstrd-stimrdt ///
imgfrnd-rfgbfml  cpimpop blncmig idetalv acetalv
sum imgetn eimgrpc imgrpc imsmetn imdfetn eimrcnt eimpcnt imrcntr impcntr imwgdwn-imacrlv ctbfsmv imrsprc pplstrd-stimrdt ///
imgfrnd-rfgbfml  cpimpop blncmig idetalv acetalv
unab varlist: imgetn eimgrpc imgrpc imsmetn imdfetn eimrcnt eimpcnt imrcntr impcntr imwgdwn-imacrlv ctbfsmv imrsprc pplstrd-stimrdt ///
imgfrnd-rfgbfml  cpimpop blncmig idetalv acetalv
display "`varlist'"

recode imgetn eimgrpc imgrpc imsmetn eimpcnt impcntr imwgdwn imhecop imfljob  ///
ctbfsmv imrsprc pplstrd vrtrlg comnlng alwspsc imgfrnd imgclg shrrfg rfgawrk gvrfgap ///
rfgfrpc rfgdtcn rfggvfn rfgbfml cpimpop blncmig idetalv acetalv $r1 pre(IMM)


*77/99
sum qfimedu-qfimcmt imtcjob-imbghct imsetbs-imdetmr lwdscwp lwpeth
unab varlist:qfimedu-qfimcmt imtcjob-imbghct imsetbs-imdetmr lwdscwp lwpeth
display "`varlist'"

recode qfimedu qfimfml qfimlng qfimchr qfimwht qfimwlt qfimwsk qfimcmt imbgeco ///
imbghct imsetbs imsetmr lwdscwp lwpeth $r2 pre(IMM)

***777/999
sum noimbro 
recode noimbro $r3 pre(IMM)

*77/99
sum imdetbs imdetmr imtcjob imbleco imbgeco imueclt imwbcnt imwbcrm
recode imdetbs imdetmr imtcjob imbleco imbgeco imueclt imwbcnt imwbcrm $r2 pre(IMINDEKS)
*7/9
sum imsmrgt imunplv  imscrlv imacrlv stimrd imdfetn eimpcnt imrcntr 
recode  imsmrgt imunplv  imscrlv imacrlv stimrd imdfetn eimpcnt imrcntr $r1 pre(IMINDEKS)

************Policy+knowledge
sum imgetn eimgrpc imgrpc imgrpc imsmetn imdfetn eimrcnt eimpcnt imrcntr impcntr  ///
imunplv  imsmrgt  imscrlv   imacrlv alwspsc stimrdt rfgawrk  gvrfgap    rfgdtcn rfggvfn rfgbfml  
recode imgetn eimgrpc imgrpc imsmetn imdfetn eimrcnt eimpcnt imrcntr impcntr  ///
imunplv  imsmrgt  imscrlv   imacrlv alwspsc stimrdt rfgawrk  gvrfgap    rfgdtcn rfggvfn rfgbfml  $r1 pre(POLICY)

recode qfimedu qfimfml  qfimlng qfimwlt qfimwsk qfimcmt lwdscwp lwpeth $r2 pre(POLICY)
recode noimbro $r3 pre(POLICY)


****************************************
foreach fff in HAPPY PERS FACT ACT VAL INT ATT IMM IMINDEKS POLICY {
egen tot`fff' = rowtotal(`fff'*)
}
keep  cntry  idno tot*
egen otherMIS = rowtotal(totHAPPY- totATT)

keep idno cntry totPOLICY otherMIS
lab var totPOLICY "Missing, Imm"
lab var otherMIS "Missing, other"

save `tf2'
use `tf1'
merge 1:1 idno cntry using `tf2' 
drop _merge
order totPOLICY otherMIS,last
tempfile tf1 tf2

*****************

drop idno

compress
save "./gendata/microdata.dta",replace
exit
